package site.wetsion.employ.server.service;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import site.wetsion.employ.server.mapper.CommentRemarkMapper;
import site.wetsion.employ.server.pojo.Company;
import site.wetsion.employ.server.pojo.CompanyComment;
import site.wetsion.employ.server.pojo.JobRemark;
import site.wetsion.employ.server.pojo.User;
import site.wetsion.employ.server.util.Page;

@Service
public class CommentRemarkService {
	
	private final static Logger log = LoggerFactory.getLogger(CommentRemarkService.class);

	@Autowired
	private CommentRemarkMapper commentRemarkMapper;
	@Autowired
	private UserService userService;
	@Autowired
	private CompanyService companyService;
	
	/** 添加公司评论*/
	public boolean addCompanyComment(CompanyComment comment){
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		comment.setGmt_create(timestamp);
		comment.setGmt_modified(timestamp);
		try {
			commentRemarkMapper.insertCompanyComment(comment);
			return true;
		} catch (Exception e) {
			log.error("插入公司评论失败"+comment.toString(), e);
			return false;
		}
	}

	/** 认为某一条评论的有用，comment_prefer加1*/
	public synchronized boolean addCommentPrefer(BigInteger id){
		try {
			commentRemarkMapper.updateCommentPrefer(id);
			return true;
		} catch (Exception e) {
			log.error("更新comment_prefer失败，comment id="+id, e);
			return false;
		}
	}
	/** 根据公司id分页获取评论*/
	public List<CompanyComment> getCommentsOfCompanyIdByPage(
			Page page, BigInteger companyId){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			List<CompanyComment> comments = commentRemarkMapper.getCommentListByCompanyIdByPage((pageNum-1)*pageSize, pageSize, companyId);
			for (CompanyComment comment : comments) {
				User user = userService.getUserById(comment.getComment_user_id());
				comment.setComment_user(user);
			}
			return comments;
		} catch (Exception e) {
			log.error("根据公司id分页获取评论失败，id="+companyId, e);
			return null;
		}
	}
	
	public List<CompanyComment> getCommentsByPage(
			Page page){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			List<CompanyComment> comments = commentRemarkMapper.getCommentListByPage((pageNum-1)*pageSize, pageSize);
			for (CompanyComment comment : comments) {
				User user = userService.getUserById(comment.getComment_user_id());
				comment.setComment_user(user);
				Company company = companyService.getCompanyById(comment.getComment_company_id());
				comment.setCompany(company);
			}
			return comments;
		} catch (Exception e) {
			log.error("获取评论失败", e);
			return null;
		}
	}
	
	/** 根据公司id获取所有的评论个数*/
	public int getCommentCountOfCompany(BigInteger companyId){
		try {
			return commentRemarkMapper.getCommentCountOfCompanyId(companyId);
		} catch (Exception e) {
			log.error("获取公司评论个数失败，公司id="+companyId, e);
			return 0;
		}
	}
	
	/** -----------------------职位评论↓--------------------*/
	/** 添加招聘职位评论*/
	public boolean addJobRemark(JobRemark comment){
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		comment.setGmt_create(timestamp);
		comment.setGmt_modified(timestamp);
		try {
			commentRemarkMapper.insertJobRemark(comment);
			return true;
		} catch (Exception e) {
			log.error("插入公司评论失败"+comment.toString(), e);
			return false;
		}
	}
	
	/** 认为某一条评论的有用，comment_prefer加1*/
	public synchronized boolean addRemarkAgree(BigInteger id){
		try {
			commentRemarkMapper.updateRemarkAgree(id);
			return true;
		} catch (Exception e) {
			log.error("更新remark_agree失败，remark id="+id, e);
			return false;
		}
	}
	/** 根据公司id分页获取评论*/
	public List<JobRemark> getRemarksOfJobIdByPage(
			Page page, BigInteger jobId){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			List<JobRemark> remarks = commentRemarkMapper.getRemarkListByJobIdByPage((pageNum-1)*pageSize, pageSize, jobId);
			for (JobRemark remark : remarks) {
				User user = userService.getUserById(remark.getRemark_user_id());
				remark.setRemark_user(user);;
			}
			return remarks;
		} catch (Exception e) {
			log.error("根据职位id分页获取评论失败，id="+jobId, e);
			return null;
		}
	}
	/** 根据公司id获取所有的评论个数*/
	public int getRemarkCountOfJob(BigInteger jobId){
		try {
			return commentRemarkMapper.getRemarkCountOfJobId(jobId);
		} catch (Exception e) {
			log.error("获取职位评论个数失败，职位id="+jobId, e);
			return 0;
		}
	}
	
}
